package com.example.jqying.androiddemo.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.jqying.androiddemo.R;

/**
 * Created by apple on 2018/6/26.
 */

public class AFragment extends Fragment {

    private TextView mTvTitle;
    private Button mBtnReset;
    private Button mBtnReplace;
    private BFragment bFragment;
    private Button mBtnMsg;
    private IOMessageClick listener;


//    public AFragment(Bundle bundle,String title){
//
//
//    }


    public static AFragment newInstance(String title){
        AFragment aFragment = new AFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        aFragment.setArguments(bundle );
        return aFragment;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //ContainterActivity实现了接口
        listener = (IOMessageClick)context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.layout_framgent_a,container,false);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mTvTitle = (TextView) (view.findViewById(R.id.tv_title));
        mBtnReplace = (Button) view.findViewById(R.id.btn_replace);
        mBtnReset = (Button) view.findViewById(R.id.btn_setText);

        if(getArguments() != null){
            mTvTitle.setText(getArguments().getString("title"));
        }

        mBtnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvTitle.setText("AFragment");

            }
        });

        mBtnReplace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bFragment == null) {
                    bFragment = new BFragment();
                }


                Fragment fragment = getFragmentManager().findFragmentByTag("a");
                if(fragment!=null){
                    getFragmentManager().beginTransaction().hide(fragment).add(R.id.fg_container, bFragment).addToBackStack(null).commitAllowingStateLoss();
                }else {
                    //添加到回退栈
                    getFragmentManager().beginTransaction().replace(R.id.fg_container, bFragment).addToBackStack(null).commitAllowingStateLoss();
                }
            }
        });

        mBtnMsg = (Button) view.findViewById(R.id.btn_msg);
        mBtnMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick("Hello From AFragment by interface");
               // ((ContainerActivity) getActivity()) .setData("Hello From AFragment");
            }
        });

    }
    public  interface IOMessageClick{
        void onClick(String text);
    }




}
